﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Entity;

namespace School.SystemManage.WebPages.Teacher
{
    public partial class LadyDeedsEdit : BasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        bool isAdd = false;
        LadyInfo li = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            isAdd = Request.QueryString["action"] == "add";

            if (!isAdd)
            {
                int lid = int.Parse(Request.QueryString["lid"]);
                li = db.LadyInfo.FirstOrDefault(l => l.lid == lid);

                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
            else
            {
                img.Visible = false;
                li = new LadyInfo();
            }
        }

        private void BindData()
        {
            if (li != null)
            {
                img.ImageUrl = li.imgPath;
                txbName.Text = li.name;
                txbrjxk.Text = li.rjxe;
                txbxrzw.Text = li.xrzw;
                txbzc.Text = li.zc;
                ddlxl.Text = li.xl;
                txbblog.Text = li.blog;
                edjj.Value = li.jj;
                edry.Value = li.ry;
                edlw.Value = li.lw;
                edkt.Value = li.kt;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (uploadImg.HasFile)
                {
                    string imgPath = string.Empty;
                    if (UploadImg(out imgPath))
                    {
                        li.imgPath = imgPath;
                    }
                    else
                    {
                        lblMsg.Text = "编辑失败，上传照片失败！";
                        return;
                    }
                }

                li.name = txbName.Text.Trim();
                li.rjxe = txbrjxk.Text.Trim();
                li.xrzw = txbxrzw.Text.Trim();
                li.zc = txbzc.Text.Trim();
                li.xl = ddlxl.SelectedItem.Text.Trim();
                li.blog = txbblog.Text.Trim();
                li.jj = edjj.Value;
                li.ry = edry.Value;
                li.lw = edlw.Value;
                li.kt = edkt.Value;

                if (isAdd)
                {
                    db.AddToLadyInfo(li);
                }

                if (db.SaveChanges() > 0)
                {
                    lblMsg.Text = "编辑成功！";
                }
                else
                {
                    lblMsg.Text = "编辑失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "编辑发生异常，原因：" + ex.Message;
            }
        }

        private bool UploadImg(out string imagePath)
        {
            imagePath = string.Empty;
            if (uploadImg.FileContent.Length > 0)
            {
                string originalFileName = uploadImg.FileName;
                string extendName = originalFileName.Substring(originalFileName.LastIndexOf('.') + 1);
                string newFileName = Guid.NewGuid().ToString() + "." + extendName;
                string serverSavePath = Server.MapPath(@"~\UploadFiles\TeacherPhone");
                imagePath = System.IO.Path.Combine(@"\UploadFiles\TeacherPhone", newFileName);
                uploadImg.SaveAs(System.IO.Path.Combine(serverSavePath, newFileName));

                return true;
            }
            return false;
        }
    }
}
